/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : join_room.js                                 
*  Description  : 조인룸 리스트                                       
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-03  김효순    Created.  
*  2012-04-04  김효순    검색바 추가          
********************************************************************************/

jQuery(function($){
	// 방장 정보
	function clsWriterInfo()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsWriterInfo.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.joinroom_profile");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("img[alt='프로필사진']").width(50).height(50).error(function(){
				$(this).attr("src", _gf.getDefaultProfileImage());
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("img[alt='프로필사진']").attr("src", this.infoData["이미지"]);
			$layer.find("dl:first dt:first").html(this.infoData["닉네임"]);
			$layer.find("dl:first dd:first ul:first li:first span:first").html(this.infoData["성별"]);
			$layer.find("dl:first dd:first ul:first li:eq(1) span:first").html(this.infoData["나이대"]);
			$layer.find("dl:first dd:first ul:first li:eq(2)").html(this.infoData["지역명"]);
			$layer.find("dl:first dd:first ul.handy li:eq(0) span").html(this.infoData["평균타수"]);
			$layer.find("dl:first dd:first ul.handy li:eq(1) span").html(this.infoData["핸디"]);
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.children().hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='position:relative; width:600px; height:70px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.children().show();
			$layer.find("div#tmpSpin").hide().spin(false);
		}
		, getMemberId: function(){
			return this.infoData["아이디"];
		}
	}
	
	// 정보
	function clsJoinRoom()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsJoinRoom.prototype = {
		_init: function(){
			$.ajax({
				url: "join_room_view02.html"
				, type: "get"
				, dataType: "html"
				, async: false
				, success: function(ret){
					var s = ret.indexOf("<div id=\"container");
					var e = ret.indexOf("<!--//   id=\"container\" 끝-->");
					var html = ret.substring(s, e);
					
					var d = document.createElement("DIV");
					
					$(d).append(html);
					
					var $joinroom_cont = $(d).find("div.joinroom_info article.joinroom_cont");
					$("div#container:first section#main_content article.joinroom_cont").after($joinroom_cont);
					$(d).remove();
				}
				, error: function(){
					alert("error");
				}
			});
			
			var $layer = $("div#container section#main_content div.joinroom_info article.joinroom_cont");
			
			this._HTMLDom = {
				"$layer": $layer
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setInfo: function(thisData){
			var $layer = this.$getLayer();
			
			this.infoData = thisData;
			
			if(this.infoData["내상태코드"] == "S"){
				$layer.eq(0).remove();
			}else{
				$layer.eq(1).remove();
			}
			
			var $layer = $("div#container section#main_content div.joinroom_info article.joinroom_cont");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.display();
		}
		, setApplyList: function(thisData){
			this.infoData["신청자"] = thisData;
			this.displayApply();
		}
		, setJoinList: function(thisData){
			this.infoData["참여자"] = thisData;
			this.displayJoin();
		}
		, display: function(){
			var $layer = this.$getLayer();
			
			$layer.find("tr:eq(0) div.time").html(this.infoData["작성일"] + "(" + this.infoData["작성요일"] + ") <span>" + this.infoData["작성시"] + "</span>");
			$layer.find("tr:eq(0) div.id").html(this.infoData["골프장코스"]);
			$layer.find("tr:eq(0) div.join_cor").html(this.infoData["홀명"] + "H");
			if(!this.infoData["비밀여부"]) $layer.find("tr:eq(0) div.secret").hide();
			
			var bName = {
				"C": "진행중"
				, "D": "조인마감"
				, "E": "조인완료"
			}
			
			$layer.find("ol.person").empty();
			for(var i=0;i<this.infoData["초대인원"];i++){
				$layer.find("ol.person").append("<li class='off'>" + (i+1) + "</li>\n");
			}
			
			if(this.infoData["내상태코드"] == "S"){
				$layer.find("tr:eq(1) td:eq(1)").html("0명");
				$layer.find("tr:eq(2) td").html(this.infoData["본인참여"] == "Y" ? "본인참여":"본인 참여하지 않음");
				
				$layer.find("tr:eq(3) td:eq(1)").html(_util.nvl(this.infoData["참여조건"]["성별"], "무관"));
				$layer.find("tr:eq(3) td:eq(3)").html(_util.nvl(this.infoData["참여조건"]["연령대"], "무관"));
				$layer.find("tr:eq(4) td:eq(1)").html(_util.nvl(this.infoData["참여조건"]["평균타수"], "무관"));
				$layer.find("tr:eq(4) td:eq(3)").html(_util.nvl(this.infoData["참여조건"]["흡연"], "무관"));
				
				
				$layer.find("tr:eq(6) td strong").html(this.infoData["예약자명"]);
				
				if(this.infoData["조인룸상태코드"] == "D"){
					$layer.find("img[alt='참여자 설정'], img[alt='초대하기']").hide();
				}
			}else{
				$layer.find("tr:eq(2) td:eq(1)").html(_util.nvl(this.infoData["참여조건"]["성별"], "무관"));
				$layer.find("tr:eq(2) td:eq(3)").html(_util.nvl(this.infoData["참여조건"]["연령대"], "무관"));
				$layer.find("tr:eq(3) td:eq(1)").html(_util.nvl(this.infoData["참여조건"]["평균타수"], "무관"));
				$layer.find("tr:eq(3) td:eq(3)").html(_util.nvl(this.infoData["참여조건"]["흡연"], "무관"));
				
				$layer.find("tr:eq(1) td.app span:first").html(0 + "명");
				$layer.find("tr:eq(1) td.app img").hide();
				if(this.infoData["조인룸상태코드"] != "D"){
					switch(this.infoData["내상태코드"]){
						case "":
						case "N": 
							$layer.find("tr:eq(1) td.app img[alt='참여신청']").show();
							break;
						case "A":
							$layer.find("tr:eq(1) td.app img[alt='신청취소']").show();
							break;
						case "P":
							$layer.find("tr:eq(1) td.app img[alt='참여취소']").show();
							break;
					}
				}
				var szState = "";
				var aState = ["미신청", "참여신청", "참여완료"];
				for(var i=0;i<aState.length;i++){
					if(szState) szState += "  →  ";
					if(aState[i] == this.infoData["내상태"]) szState += "<span>" + aState[i] + "</span>";
					else szState += aState[i];
				}
				
				$layer.find("tr.joinroom_state td.my_state").html(szState);
				$layer.find("tr.joinroom_state td:eq(1)").find("img").hide().filter("img[alt='" + bName[this.infoData["조인룸상태코드"]] + "']").show();
			}
			
			$layer.find("tr:last td.txt_con div").html(this.infoData["인사말"]);
		}
		, displayApply: function(){
			var $layer = this.$getLayer();
			var d = this.infoData["신청자"];
			
			if(this.infoData["내상태코드"] == "S"){
				$layer.find("tr:eq(1) td:eq(1)").html(d.length + "명").hover(function(){
					if(d.length) _gf.showMemberLayer($(this), d, "left", 0, $(this).height());
				}, function(){
					_gf.closeMemberLayer();
				});
			}else{
				$layer.find("tr:eq(1) td.app span:first").html(d.length + "명").hover(function(){
					if(d.length) _gf.showMemberLayer($(this), d, "left", 0, $(this).height());
				}, function(){
					_gf.closeMemberLayer();
				});
			}
		}
		, displayJoin: function(){
			var $layer = this.$getLayer();
			var d = this.infoData["참여자"];
			
			$layer.find("ol.person").hover(function(){
				if(d.length) _gf.showMemberLayer($(this), d, "right", -115, $(this).height());
			}, function(){
				_gf.closeMemberLayer();
			}).find("li").filter(":lt(" + d.length + ")").removeClass("off").addClass("on");
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.hide();
			if($layer.eq(0).prev("div#tmpSpin").length == 0){
				$layer.eq(0).before("<div id='tmpSpin' style='position:relative; width:700px; height:100px;' />");
			}
			$layer.eq(0).prev("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.show();
			$layer.prev("div#tmpSpin").hide().spin(false);
		}
	}
	
	// 추천회원
	function clsRecommList()
	{
		this._HTMLDom = null;
		this.listData = null;
		this.inviteCount = null;
		
		this._ERRMSG = {
			"_NODATA": "초대할 회원이 없습니다."
			, "_NOVALUE": "초대할 회원을 선택해 주십시요."

		}
		this.errMsg = null;
	}
	
	clsRecommList.prototype = {
		init: function(){
			var $layer = $("div#container section#main_content div.joinroom_info article.joinroom_cont tr:eq(5)");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("div.invite_list label:first").clone(true)
			}
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, setMemberInfo: function(i, thisData){
			this.listData[i] = thisData;
		}
		, setInviteCount: function(inviteCount){
			this.inviteCount = inviteCount;
			this.displayInviteCount();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("div.invite_list").empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("div.invite_list").append($element.clone(true));
			}
			$layer.find("div.invite_list label").each(function(n){
				$(this).find("span").html(thisClass.listData[n]["닉네임"]).hover(function(){
					_gf.showProfileLayer($(this), thisClass.listData[n], "left", 0, $(this).height());
				}, function(){
					_gf.closeProfileLayer();
				});
			});
		}
		, displayInviteCount: function(){
			var $layer = this.$getLayer();
			
			$layer.find("td p:first em:first").html("(" + this.inviteCount + ")");
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.invite_list").hide();
			
			if($layer.find("div#tmpSpin").length == 0){
				$layer.find("div.invite_list").after("<div id='tmpSpin' style='position:relative; width:580px; height:100px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div#tmpSpin").hide().spin(false);
			$layer.find("div.invite_list").show();
		}
		, $getBtnInvite: function(){
			return this.$getLayer().find("img[alt='초대하기']");
		}
		, $getBtnInvited: function(){
			return this.$getLayer().find("td p:first em:first");
		}
		, getError: function(){
			if(this.errMsg) return this._ERRMSG[this.errMsg];
			else return null;
		}
		, valid: function(){
			var $layer = this.$getLayer();
			var ckCount = $layer.find("input:checkbox").length;
			var ckedCount = $layer.find("input:checkbox:checked").length;
			
			this.errMsg = null;

			if(ckCount == 0){
				this.errMsg = "_NODATA";
				return false;
			}else if(ckedCount == 0){
				this.errMsg = "_NOVALUE";
				return false;
			}
			
			return true;
		}
		, getMembersValue: function(){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			var szRet = "";
			$layer.find("input:checkbox:checked").each(function(){
				var n = $layer.find("input:checkbox").index($(this));
				
				if(szRet) szRet += ",";
				szRet += thisClass.listData[n]["아이디"];
			});
			
			return szRet;
		}
		, getCheckCnt: function(){
			var $layer = this.$getLayer();
			
			return $layer.find("input:checkbox:checked").length;
		}
	
	}
	
	// 댓글리스트
	function clsJoinroomCommentList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsJoinroomCommentList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.joinroom_reply");
			$layer.find("dl:first>dt:first img").error(function(){
				$(this).attr("src", _gf.getDefaultProfileImage());
			});
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("dl:first").clone(true)
			}
			
			this.setList([]);
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, setWriterInfo: function(n, info){
			for(var key in info){
				this.listData[n]["작성자"][key] = info[key];
			}
			
			this.displayWriter(n);
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("p.reply_num span").html("(" + this.listData.length + ")");
			
			$layer.find("dl").remove();
			
			for(var i=0;i<this.listData.length;i++){
				$layer.find("div.reply_regist").before($element.clone(true));
			}
			$layer.find("dl").each(function(n){
				if(n > 0) $(this).attr("class", "");
				
				$(this).find(">dt:first img").attr("src", thisClass.listData[n]["작성자"]["이미지"]);
				$(this).find(">dd:first p.nickname a").html(thisClass.listData[n]["작성자"]["닉네임"] ? thisClass.listData[n]["작성자"]["닉네임"]:"").hover(function(){
					_gf.showProfileLayer($(this), thisClass.listData[n]["작성자"], "left", 0, $(this).height());
				}, function(){
					_gf.closeProfileLayer();
				});
				$(this).find("p.reply_date").html(thisClass.listData[n]["작성일"] + " <span>" + thisClass.listData[n]["작성시"] + "</span>");
				$(this).find(">dd:first div").html(thisClass.listData[n]["내용"]);
				
				if(!thisClass.listData[n]["내꺼여부"]) $(this).find("img[alt='삭제']").hide();
			});
			
			
		}
		, displayWriter: function(n){
			var $obj = this._HTMLDom["$layer"].find("dl:eq(" + n + ")");
			
			$obj.find(">dt:first img").attr("src", _gf.filePath2Url(this.listData[n]["작성자"]["이미지"]));
			$obj.find(">dd:first p.nickname a").html(this.listData[n]["작성자"]["닉네임"])
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("dl").hide();
			if($layer.find("div.reply_regist:last").prev("div#tmpSpin").length == 0){
				$layer.find("div.reply_regist:last").before("<div id='tmpSpin' style='position:relative; width:750px; height:100px;' />");
			}
			
			$layer.find("div.reply_regist:last").prev("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.find("div.reply_regist:last").prev("div#tmpSpin").hide().spin(false);
			
			$layer.find("dl").show();
		}
		, $getBtnsDelete: function(){
			return this._HTMLDom["$layer"].find("dl img[alt='삭제']");
		}
		, getKey: function(n){
			return this.listData[n]["댓글키"];
		}
	}
	
	// 댓글등록
	function clsCommentRegist()
	{
		this._HTMLDom = null;
		this.bAuth = null;
		
		this._init();
	}
	
	clsCommentRegist.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.joinroom_reply div.reply_regist");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.css("position", "relative");
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, reset: function(){
			this.$getLayer().find("textarea").val("");
		}
		, focus: function(){
			this.$getLayer().find("textarea").trigger("focus");
		}
		, getValue: function(){
			return this.$getLayer().find("textarea").val();
		}
		, setAuth: function(){
			this.bAuth = true;
		}
		, getAuth: function(){
			return this.bAuth;
		}
		, valid: function(){
			var $layer = this.$getLayer();
			
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.children().hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.prepend("<div id='tmpSpin' style='position:relative; width:580px; height:100px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.children().show();
			$layer.find("div#tmpSpin").hide().spin(false);
		}
	}
	
	function clsButtons()
	{
		this._HTMLDom = null;
		this.szState = null;
		
		this._init();
	}
	
	clsButtons.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.btn_area_box");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setState("");
		}
		, setState: function(val){
			var $layer = this._HTMLDom["$layer"];
			var thisClass = this;
			
			this.szState = val;
			
			if(this.szState == "부킹장"){
				$("body").prepend("<div />").find("div:first").load("join_room_view02.html article.btn_area", function(){
					var $newLayer = $("body div:first article.btn_area").clone(true);
					$("body div:first").remove();
					
					$layer.after($newLayer).remove();
					
					thisClass._HTMLDom = {
						"$layer": $newLayer
					}
					
					document.objControl.buttonsBind();
				});
			}
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
	}
	
	function clsComm()
	{
		// 방장정보
		this.writerInfo = null;
		
		// 조인룸 정보
		this.joinroomData = null;
		this.joinroomRequest = null;
		
		// 추천회원 리스트
		this.recommList = null;
		
		// 댓글
		this.joinroomCommentRequest = null;
		this.joinroomCommentList = null;
		
		// 지역정보
		this.areaList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.joinroomData = {
				"날짜": null
				, "작성자": null
				, "코스명": null
				, "홀명": null
				, "비밀여부": null
				, "인사말": null
				, "참여자": []
				, "나의상태": null
				, "신청자": []
			}
			
			this.joinroomRequest = {
				"키": null
			}
			
			this.joinroomCommentRequest = {
				"조인룸코드": null
			}
			
			this.joinroomCommentList = [
				{
					"댓글키": null
					, "이미지": null
					, "작성자": {
						"아이디": null
						, "닉네임": null
						, "이미지": null
						, "성별": null
						, "핸디": null
						, "비거리": null
					}
					, "내용": null
					, "작성일": null
					, "작성시": null
				}
			];
			
			this.joinroomApplyList = [
				{
					"아이디": null
					, "닉네임": null
					, "이미지": null
					, "성별": null
					, "핸디": null
					, "비거리" : null
				}
			];
			
			this.joinroomJoinList = [
				{
					"아이디": null
					, "닉네임": null
				}
			];
		}
		, setJoinroomRequest: function(key, val){
			if(typeof key == "object"){
				this.joinroomRequest = key;
			}else{
				this.joinroomRequest[key] = val;
			}
		}
		// 조인룸 정보
		, getJoinroomInfo: function(arg){
			var thisClass = this;
			
			arg = arg || {}
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "getDetailJoinRoom";
			_gf.ajax({
				"sid":"JR001",
				"servicename":serviceName,
				"listcount":1,
				"currpageno":1
			}, {
				"joinRoomSeq": this.joinroomRequest["키"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				if(res["결과코드"] == "00000"){
					var obj = ret["msp"]["contents"]["items"]

					var myState = "미신청";
					switch(obj["joinStatus"]){
						case "S":
							myState = "부킹장";
							break;
						case "A":
							myState = "참여신청";
							break;
						case "P":
							myState = "참여완료";
							break;
					}
					
					var szStat = "";
					switch(obj["procStatus"].trim()){
						case "C":
							szStat = "진행중";
							break;
						case "D":
							szStat = "조인마감";
							break;
						case "E":
							szStat = "조인완료";
							break;
					}
					
					var szSmoke = "";
					switch(_util.nvl(obj["smokeYn"]).trim()){
						case "Y":
							szSmoke = "흡연";
							break;
						case "N":
							szSmoke = "비흡연";
							break;
					}
					
					thisClass.joinroomData = {
						"작성일": _util.nvl(obj["reserveDate"])
						, "작성요일": _util.nvl(obj["bookingDay"])
						, "작성시": _util.nvl(obj["bookingTime"])
						, "작성자": _util.nvl(obj["bookUserId"])
						, "골프장코스": _util.nvl(obj["golfClubInfo"])
						, "홀명": _util.nvl(obj["holeInfo"])
						, "비밀여부": (obj["openYn"] == "Y") ? false:true
						, "인사말": _util.enter2br(_util.nvl(obj["ment"]))
						, "내상태코드": _util.nvl(obj["joinStatus"])
						, "내상태": myState
						, "신청자수": _util.nvl(obj["applycnt"])+"명"
						, "참여자수": _util.nvl(obj["particnt"])+"명"
						, "본인참여": _util.nvl(obj["joinYn"]).trim()
						, "참여조건": {
							"성별": _util.nvl(obj["sex"]).trim()
							, "연령대": _util.nvl(obj["age"]).trim()
							, "평균타수": _util.nvl(obj["aVerage"]).trim()
							, "흡연": szSmoke
						}
						, "진행상황": szStat
						, "초대회원수": _util.nvl(obj["memCount"])
						, "예약자명": _util.nvl(obj["rUserName"])
						, "조인룸상태코드": _util.nvl(obj["procStatus"])
						, "조인룸상태": _util.nvl(obj["procStatusNm"])
						, "초대인원": _util.nvl(obj["invitCnt"], 0)
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
		, getJoinroomData: function(){
			return this.joinroomData;
		}
		, setJoinroomCommentRequest: function(key, val){
			if(typeof key == "object"){
				this.joinroomCommentRequest = key;
			}else{
				this.joinroomCommentRequest[key] = val;
			}
		}
		// 댓글 리스트
		, getJoinroomCommentList: function(){
			var thisClass = this;
			
			//ajax
			var serviceName = "getReplyList";
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
				, "listcount": 10
				, "currpageno": 1
			}, {
				"joinRoomSeq": this.joinroomCommentRequest["조인룸코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "전체수": ret["msp"]["contents"]["@totalcount"]
				}
				
				thisClass.joinroomCommentList = [];
				var totalCount = 0;
				
				if(res["결과코드"] == "00000"){
					var items = ret["msp"]["contents"]["items"];
					if(items){
						if(!items.length){
							items = new Array(items);
						}
						
						for(var i=0;i<items.length;i++){
							var obj = items[i];
							var regDate = obj["regDate"] + "";
							var regTime = obj["regTime"] + "";
							
							thisClass.joinroomCommentList.push({
								"댓글키": obj["replySeq"]
								, "내꺼여부": (obj["replyAuth"] == 0 ? true:false)
								, "내용": _util.enter2br(_util.nvl(obj["content"]))
								, "작성자": {
									"아이디": obj["regId"]
									, "닉네임": obj["nickName"]
									, "이미지": ""
								}
								, "작성일": regDate.substr(0, 4) + "." + regDate.substr(4,2) + "." + regDate.substr(6,2)
								, "작성시": regTime.substr(0, 2) + ":" + regTime.substr(2, 2)
							});
						}
					}
				}
				
				document.objControl.finishJoinroomCommentList(res);
			});
		}
		, getJoinroomCommentData: function(){
			return this.joinroomCommentList;
		}
		// 조인룸삭제
		, joinroomDelete: function(joinroomKey){
			var thisClass = this;
			
			//ajax
			var serviceName = "deleteJoinRoom";
			var options = {
				"joinRoomSeq": joinroomKey
			}
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishJoinroomDelete(res);
			});
		}
		// 방장정보
		, getWriterInfo: function(arg){
			var thisClass = this;
			
			// ajax
			_gf.objComm.getMemberInfo({
				"아이디": arg["아이디"]
				, "콜백": function(ret, info){
					thisClass.writerInfo = info;
					document.objControl.finishWriterInfo(ret);
				}
			});
		}
		, getWriterData: function(){
			return this.writerInfo;
		}
		// 추천회원
		, getRecommList: function(){
			var thisClass = this;
			
			//ajax
			var serviceName = "getRecommendMemberList";
			_gf.ajax({
				"sid":"JR001",
				"servicename":serviceName,
				"listcount":10,
				"currpageno":1
			}, {
				"joinRoomSeq": this.joinroomRequest["키"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				thisClass.recommList = [];
				
				if(res["결과코드"] == "00000"){
					var items = ret["msp"]["contents"]["items"];
					if(items){
						if(!items.length){
							items = new Array(items);
						}
						
						var gender = {
							"M": "남자"
							, "F": "여자"
						}
						
						for(var i=0;i<items.length;i++){
							var obj = items[i];
							
							thisClass.recommList.push({
								"아이디": _util.nvl(obj["userId"])
								, "닉네임": _util.nvl(obj["nickName"])
								, "이미지": ""
								, "지역코드": _util.nvl(obj["region"])
								, "나이코드": _util.nvl(obj["age"])
								, "성별코드": _util.nvl(obj["sex"])
								, "성별": gender[_util.nvl(obj["sex"])]
								, "핸디": _util.nvl(obj["handi"], 0)
								, "평균타수": _util.nvl(obj["aVerage"], 0)
							});
						}
					}
				}
				
				document.objControl.finishRecommList(res);
			});
		}
		, getRecommData: function(){
			return this.recommList;
		}
		// 댓글등록
		, joinroomCommentSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "registReply";
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
				, "listcount": 10
				, "currpageno": 1
			}, {
				"joinRoomSeq": arg["조인룸코드"]
				, "content": encodeURIComponent(arg["내용"])
			}, function(ret){
				ret["결과코드"] = ret["msp"]["contents"]["serviceResult"];
				ret["메시지"] = _gf.getResultMsg(ret);
				
				document.objControl.finishJoinroomCommentSubmit(ret);
			});
		}
		// 댓글삭제
		, joinroomCommentDelete: function(arg){
			var thisClass = this;
			
			// ajax
			var serviceName = "deleteReply";
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
				, "listcount": 1
				, "currpageno": 1
			}, {
				"joinRoomSeq": arg["조인룸코드"]
				, "replySeq": arg["댓글키"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishJoinroomCommentDelete(res);
			});
		}
		// 초대하기
		, inviteSubmit: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "invitMemberRegist";
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
			}, {
				"joinRoomSeq": arg["조인룸코드"]
				, "initId": arg["아이디들"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "아이디들": arg["아이디들"]
				}
				
				document.objControl.finishInviteSubmit(res);
			});
		}
		// 참여신청
		, joinroomApply: function(arg){
			var thisClass = this;

			//ajax
			var serviceName = "registMember";
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
				, "listcount": 1
				, "currpageno": 1
			}, {
				"joinRoomSeq": arg["조인룸코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishJoinroomApply(res);
			});
		}
		// 신청자
		, getApplyList: function(arg){
			var thisClass = this;
			
			//ajax
			document.objGlobal.ajax({
				"sid":"JR001",
				"servicename":"getApplyMemberList",
				"listcount":10,
				"currpageno":1
			}, {
				"joinRoomSeq": arg["조인룸코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						res["결과값"].push({
							"아이디": _util.nvl(obj["participantId"])
							, "닉네임": _util.nvl(obj["nickName"])
							, "이미지": _util.nvl(obj["path"]) + _util.nvl(obj["saveFileNm"])
						});
					}
				}
				
				document.objControl.finishApplyList(res);
			});
		}
		// 참여자
		, getJoinList: function(arg){
			var thisClass = this;

			//ajax
			document.objGlobal.ajax({
				"sid":"JR001",
				"servicename":"getMemberList",
				"listcount":10,
				"currpageno":1
			}, {
				"joinRoomSeq": arg["조인룸코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						res["결과값"].push({
							"아이디": _util.nvl(obj["participantId"])
							, "닉네임": _util.nvl(obj["nickName"])
							, "이미지": _util.nvl(obj["path"]) + _util.nvl(obj["saveFileNm"])
						});
					}
				}

				document.objControl.finishJoinList(res);
			});
		}
		// 신청취소
		, applyCancel: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "ApplyCancel";
			var options = {
				"joinRoomSeq": arg["조인룸코드"]
			}
			_gf.ajax({
				"sid": "JR001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishApplyCancel(res);
			});
		}
	}
	
	function clsControl(){
		this.objComm = null;
		this.objWriterInfo = null;
		this.objJoinroom = null;
		this.objJoinroomCommentList = null;
		this.objButtons = null;
		this.objRecommList = null;
		this.objCommentRegist = null;
		
		this.q_my = _util.getParam("q_my");
		this.joinroomKey = _util.getParam("q_seq");
		this.cJoinroomKey = _util.getCookie("joinroomKey");
		
		this.areaCodeList = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objWriterInfo = new clsWriterInfo();
			this.objJoinroom = new clsJoinRoom();
			this.objJoinroomCommentList = new clsJoinroomCommentList();
			this.objButtons = new clsButtons();
			this.objRecommList = new clsRecommList();
			this.objCommentRegist = new clsCommentRegist();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
			
			if(!this.joinroomKey){
				this.goListPage();
			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return (_gf.getAreaCodeData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 방장 바인드
			this.objWriterInfo.$getLayer().on("click", "img[alt='쪽지']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "쪽지":
						_gf.popNote(thisClass.objWriterInfo.getMemberId());
						break;
				}
			});
			
			// 목록클릭
			this.objButtons.$getLayer().on("click", "img[alt='목록']", function(){
				thisClass.goListPage();
			});
			
			this.objCommentRegist.$getLayer().on("focus", "textarea", function(){
				if(!_util.getCookie("member_id")){
					if(!confirm(_LOGIN_TEXT)){
						$(this).trigger("blur");
						return;
					}
					_gf.goLogin();
					return;
				} else if(!thisClass.objCommentRegist.getAuth()){
					alert("조인룸 신청자 또는 참여자만 댓글작성이 가능합니다.");
					$(this).trigger("blur");
					return;
				}
			});
			
			// 댓글등록
			this.objCommentRegist.$getLayer().on("click", "img[alt='댓글등록']", function(){
				if(!_util.getCookie("member_id")){
					if(!confirm(_LOGIN_TEXT)) return;
					_gf.goLogin();
					return;
				} else if(!thisClass.objCommentRegist.getAuth()){
					alert("조인룸 신청자 또는 참여자만 댓글작성이 가능합니다.");
					return;
				}else if(!_gf.validComment(thisClass.objCommentRegist.getValue())){
					thisClass.objCommentRegist.focus();
					return;
				}
				
				thisClass.objCommentRegist.spinStart();
				
				thisClass.objComm.joinroomCommentSubmit({
					"조인룸코드": thisClass.joinroomKey
					, "내용": thisClass.objCommentRegist.getValue()
				});
			});
			
			// 댓글삭제
			this.objJoinroomCommentList.$getBtnsDelete().live("click", function(){
				if(!confirm("삭제하시겠습니까?")) return;
				
				var n = thisClass.objJoinroomCommentList.$getBtnsDelete().index($(this));
				var key = thisClass.objJoinroomCommentList.getKey(n);
				
				thisClass.objJoinroomCommentList.spinStart();
				thisClass.objComm.joinroomCommentDelete({
					"조인룸코드": thisClass.joinroomKey
					, "댓글키": key
				});
			});
			
			this.objJoinroom.$getLayer().on("click", "img[alt='참여신청'], img[alt='참여취소']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "참여신청":
						if(!_util.getCookie("member_id")){
							if(!confirm(_LOGIN_TEXT)) return;
							_gf.goLogin();
							return;
						}
						
						_gf.spinStart("sending...");
						thisClass.objComm.joinroomApply({
							"조인룸코드": thisClass.joinroomKey
						});
						break;
					case "참여취소":
						if(!confirm("신청을 취소하시겠습니까?")) return;
						
						_gf.spinStart("sending...");
						thisClass.objComm.applyCancel({
							"조인룸코드": thisClass.joinroomKey
						});
						break;
				}
			});
			
			// 조인룸 만들기
			$(document).on("click", "img[alt='조인룸만들기']", function(){
				if(!_util.getCookie("member_id")){
					if(!confirm(_LOGIN_TEXT)) return;
					_gf.goLogin();
					return;
				}
				
				thisClass.goNew();
			});
		}
		, _start: function(){
			var thisClass = this;
			
			this.areaCodeList = _gf.getAreaCodeData();
			
			// 조인룸 정보 요청
			//this.objCommentRegist.$getLayer().hide();
			this.objJoinroom.spinStart();
			this.objComm.setJoinroomRequest("키", this.joinroomKey);
			this.objComm.getJoinroomInfo({
				"콜백": function(ret){
					thisClass.finishJoinroomInfo(ret);
				}
			});
			
			// 댓글 요청
			this.objJoinroomCommentList.spinStart();
			this.objComm.setJoinroomCommentRequest("조인룸코드", this.joinroomKey);
			this.objComm.getJoinroomCommentList();
		}
		, buttonsBind: function(){
			var thisClass = this;
			
			// 목록클릭
			this.objButtons.$getLayer().on("click", "img", function(){
				var altName = $(this).attr("alt");
				
				switch(altName){
					case "목록":
						thisClass.goListPage();
						break;
					case "수정":
						thisClass.goModifyPage();
						break;
					case "조인룸 삭제":
						if(!confirm("삭제하시겠습니까?")) return;
				
						_gf.spinStart("deleting...");
						thisClass.objComm.joinroomDelete(thisClass.joinroomKey);
						break;
				}
			});
			
			// 조인룸 만들기
			$("div#container section#main_content").on("click", "img[alt='조인룸만들기']", function(){
				thisClass.goNew();
			});
		}
		, bookingjjangBind: function(){
			var thisClass = this;
			
			// 초대하기 클릭
			this.objRecommList.$getBtnInvite().click(function(){
				if(!thisClass.objRecommList.valid()){
					alert(thisClass.objRecommList.getError());
					return;
				}
				
				var members = thisClass.objRecommList.getMembersValue();
				var cnt = thisClass.objRecommList.getCheckCnt();
				var jc = "";
				for(var i=0;i<cnt;i++){
					if(jc) jc += ",";
					jc += thisClass.joinroomKey;
				}
				
				thisClass.objRecommList.spinStart();
				thisClass.objComm.inviteSubmit({
					"조인룸코드": jc
					, "아이디들": members
				});
			});
			
			// 초대자 클릭
			this.objRecommList.$getBtnInvited().css("cursor", "pointer").click(function(){
				var winInvite = window.open(_ROOT + "/popup/pop_invite.html?q_joinroomKey=" + thisClass.joinroomKey, "winInvite", "width=600, height=410, scrollbars=0");
				if(winInvite) winInvite.focus();
			});
			
			// 참여자 설정
			this.objJoinroom.$getLayer().on("click", "img[alt='참여자 설정']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "참여자 설정":
						var winJoin = window.open(_ROOT + "/popup/pop_join.html", "winJoin",  "width=1000, height=410, scrollbars=0");
						if(winJoin) winJoin.focus();
						break;
				}
			});
		}
		// 조인룸 정보 완료
		, finishJoinroomInfo: function(ret){
			var thisClass = this;
			var thisData = this.objComm.getJoinroomData();
			
			this.objJoinroom.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objJoinroom.setInfo(thisData);
			
			// 신청자 요청
			this.objComm.getApplyList({
				"조인룸코드": this.joinroomKey
			});
			
			// 참여자 요청
			this.objComm.getJoinList({
				"조인룸코드": this.joinroomKey
			});
			
			if(thisData["내상태코드"] == "S"){
				this.objRecommList.init();
				this.objRecommList.setInviteCount(thisData["초대회원수"]);
				
				this.bookingjjangBind();
				
				this.objRecommList.spinStart();
				this.objComm.getRecommList();
			}else{
				if(thisData["비밀여부"] && thisData["내상태"] != "신청완료" && thisData["내상태"] != "참여완료"){
					if(this.joinroomKey != this.cJoinroomKey){
						this.goListPage();
						return;
					}
				}
			}
			
			if(thisData["내상태코드"] == "S" || thisData["내상태코드"] == "I" || thisData["내상태코드"] == "A" || thisData["내상태코드"] == "P") this.objCommentRegist.setAuth();
			
			// 방장 정보요청
			document.objUtil.executeThread(function(){
				return thisClass.areaCodeList;
			}, function(){
				thisClass.objWriterInfo.spinStart();
				thisClass.objComm.getWriterInfo({
					"아이디": thisData["작성자"]
				});
			});
			
			// 신청자 리스트 요청
			//this.objComm.getJoinroomApplyList(this.joinroomKey);
			// 참여자 리스트 요청
			//this.objComm.getJoinroomJoinList(this.joinroomKey);
			
			this.objButtons.setState(thisData["내상태"]);
			
		}
		// 댓글 리스트 완료
		, finishJoinroomCommentList: function(ret){
			var thisClass = this;
			
			this.objJoinroomCommentList.spinEnd();
			
			var thisData = this.objComm.getJoinroomCommentData();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objJoinroomCommentList.setList(thisData);
			
			for(var i=0;i<thisData.length;i++){
				_gf.getMemberInfo({
					"댓글인덱스": i
					, "아이디": thisData[i]["작성자"]["아이디"]
					, "콜백": function(ret, info, args){
						thisClass.objJoinroomCommentList.setWriterInfo(args["댓글인덱스"], info);
					}
				});
			}
		}
//		// 조인룸 참여자 취소 완료
//		, finishJoinroomJoinCancel: function(ret){
//			// 참여자 리스트 요청
//			this.objComm.getJoinroomJoinList(this.joinroomKey);
//		}
//		// 참여승인 완료
//		, finishJoinroomJoinSubmit: function(ret){
//			// 참여자 리스트 요청
//			this.objComm.getJoinroomJoinList(this.joinroomKey);
//			// 신청자 리스트 요청
//			this.objComm.getJoinroomApplyList(this.joinroomKey);
//		}
		// 조인룸 삭제 완료
		, finishJoinroomDelete: function(ret){
			_gf.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.goListPage();
		}
		// 방장 완료
		, finishWriterInfo: function(ret){
			var thisData = this.objComm.getWriterData();
			
			for(var i=0;i<this.areaCodeList.length;i++){
				if(this.areaCodeList[i]["키"] == thisData["지역코드"]) break;
			}
			
			if(i<this.areaCodeList.length){
				thisData["지역"] = this.areaCodeList[i]["내용"]
			}
			
			this.objWriterInfo.spinEnd();
			this.objWriterInfo.setInfo(thisData);
		}
		// 추천회원리스트
		, finishRecommList: function(ret){
			var thisClass = this;
			var thisData = this.objComm.getRecommData();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objRecommList.spinEnd();
			this.objRecommList.setList(thisData);
			
			for(var i=0;i<thisData.length;i++){
				_gf.getMemberInfo({
					"아이디": thisData[i]["아이디"]
					, "인덱스": i
					, "콜백": function(res, info, arg){
						thisClass.objRecommList.setMemberInfo(arg["인덱스"], info);
					}
				});
			}
		}
		// 댓글완료
		, finishJoinroomCommentSubmit: function(ret){
			this.objCommentRegist.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objCommentRegist.reset();
			
			// 댓글리스트
			this.objJoinroomCommentList.spinStart();
			this.objComm.setJoinroomCommentRequest("조인룸코드", this.joinroomKey);
			this.objComm.getJoinroomCommentList();
		}
		// 댓글삭제 완료
		, finishJoinroomCommentDelete: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objComm.setJoinroomCommentRequest("조인룸코드", this.joinroomKey);
			this.objComm.getJoinroomCommentList();
		}
		, getJoinroomKey: function(){
			return this.joinroomKey;
		}
		// 초대완료
		, finishInviteSubmit: function(ret){
			var thisClass = this;
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objComm.getRecommList();
			
			this.objComm.getJoinroomInfo({
				"콜백": function(ret){
					thisClass.finishJoinroomInfoInviteCnt(ret);
				}
			});
		}
		// 초대 후 조인룸 정보 완료
		, finishJoinroomInfoInviteCnt: function(ret){
			var thisData = this.objComm.getJoinroomData();
			
			this.objRecommList.setInviteCount(thisData["초대회원수"]);
		}
		// 조인룸 참여완료
		, finishJoinroomApply: function(ret){
			_gf.spinEnd();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			document.location.reload();
		}
		// 신청자 완료
		, finishApplyList: function(ret){
			if(ret["결과코드"] != "00000") return;
			
			this.objJoinroom.setApplyList(ret["결과값"]);
		}
		// 참여자 완료
		, finishJoinList: function(ret){
			if(ret["결과코드"] != "00000") return;
			
			this.objJoinroom.setJoinList(ret["결과값"]);
		}
		// 신청취소 완료
		, finishApplyCancel: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			_gf.spinEnd();
			
			document.location.reload();
		}
		// 목록이동
		, goListPage: function(){
			document.location.href = "join_room.html" + _util.paramStringFilter(["q_seq"]);
		}
		// 수정이동
		, goModifyPage: function(){
			document.location.href = "join_room_creat01.html" + document.location.search;
		}
		// 만들기 이동
		, goNew: function(){
			document.location.href = "join_room_creat01.html" + _util.paramStringFilter(["q_seq"]);
		}
		, closeJoin: function(w){
			if(w) w.close();
			
			document.location.reload();
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});